package com.acane.instagram.service;

import com.acane.instagram.mapper.SensitiveWordMapper;
import com.acane.instagram.utils.ACTrie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

// SensitiveWordService.java 热更新逻辑
@Service
public class SensitiveWordService {

    @Autowired
    private SensitiveWordMapper sensitiveWordMapper;
    private volatile ACTrie acTrie;

    @PostConstruct
    public void init() {
        reloadTrie();
    }

    public void reloadTrie() {
        List<String> words = sensitiveWordMapper.getAllWords();
        this.acTrie = new ACTrie(words);
    }

    public boolean containsSensitive(String text) {
        return acTrie.containsSensitiveWord(text);
    }
}